<template>
    <div class="big_box">
        <el-breadcrumb separator="/">
            <el-breadcrumb-item>物资管理</el-breadcrumb-item>
            <el-breadcrumb-item>物资分类</el-breadcrumb-item>
        </el-breadcrumb>
        <el-divider></el-divider>
        <el-row style="padding-bottom: 10px">
            <el-col :span="12">
                <el-button-group>
                    <el-button class="buttongroup"  type="success" icon="el-icon-circle-plus-outline" @click="AddDeviceCategory_dialog=true">新增</el-button>
                    <el-button class="buttongroup" type="danger" icon="el-icon-delete" @click="BatchDelete_event">批量删除</el-button>
                </el-button-group>
            </el-col>
            <el-col :offset="6" :span="6" align="right">
                <el-input placeholder="请输入搜索标题" v-model="Like">
                    <el-button slot="append" icon="el-icon-search" @click="formdata"></el-button>
                </el-input>
            </el-col>
        </el-row>
        <el-row style="margin-top: 10px">
            <el-col :span="24">
                <el-table :data="tableData" border stripe style="width: 100%" @selection-change="SelectionChange_event">
                    <el-table-column type="selection" width="40" align="center"></el-table-column>
                    <el-table-column fixed type="index" label="序号" width="50" align="center" :index="indexMethod"></el-table-column>
                    <el-table-column fixed prop="DeviceCategoryName" label="名称" align="center"></el-table-column>
                    <el-table-column fixed prop="ParentId" label="父级分类" width="120" align="center">
                        <template slot-scope="slot">
                            {{ GetParentDeviceCategory(slot.row.ParentId) }}
                        </template>
                    </el-table-column>
                    <el-table-column prop="Code" label="编码"  align="center"></el-table-column>
                    <el-table-column prop="DeviceCategoryDescript" label="备注"  align="center"></el-table-column>
                    <el-table-column fixed="right" label="操作" width="145" align="center">
                        <template slot-scope="scope">
                            <el-button @click="handleClick(scope.row)" type="primary" size="mini">编辑</el-button>
                            <el-button type="danger" size="mini" @click="BatchDeleteOne_event(scope.row)">删除</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
            <el-col :span="24">
                <div class="block">
                    <el-pagination
                            @size-change="formdata"
                            @current-change="formdata"
                            :current-page.sync="pagination.currentPage"
                            :page-sizes="pagination.pagesizes"
                            :page-size.sync="pagination.pagesize"
                            layout="total, sizes, prev, pager, next, jumper"
                            :total.sync="pagination.total">
                    </el-pagination>
                </div>
            </el-col>
        </el-row>

        <el-dialog :visible.sync="AddDeviceCategory_dialog" @close="AddDeviceCategory_close" width="30%" title="添加物品分类信息">
            <AddDeviceCategory ref="AddDeviceCategory_ref" :all-device-category="AllDeviceCategory"></AddDeviceCategory>
        </el-dialog>

        <el-dialog :visible.sync="UpdataDeviceCategory_dialog" width="30%" title="添加物品分类信息">
            <UpdataDeviceCategory :all-device-category="AllDeviceCategory" :device-category="DeviceCategory_details"></UpdataDeviceCategory>
        </el-dialog>

    </div>
</template>

<script>
    import * as publicjs from "/public/Scriput/Public";
    import AddDeviceCategory from "./AddDeviceCategory";
    import UpdataDeviceCategory from "./UpdataDeviceCategory";

    export default {
        name: "DeviceCategory",
        components: {
            'AddDeviceCategory': AddDeviceCategory,
            'UpdataDeviceCategory': UpdataDeviceCategory,
        },
        data() {
            return {
                //表格数据
                tableData: [],
                //分页数据绑定
                pagination: {
                    currentPage: 1,
                    pagesizes: [10, 20, 30, 40, 50],
                    pagesize: 10,
                    total: 100
                },
                AllDeviceCategory: [],
                Like: '',
                //表格选中的数据
                SelectMore_lis: [],
                // 添加弹框显示
                AddDeviceCategory_dialog: false,
                // 编辑弹框显示
                UpdataDeviceCategory_dialog: false,
                // 单条数据详情
                DeviceCategory_details: {},
            }
        },
        created() {
            this.AllDeviceCategory = publicjs.GetAllDeviceCategory(this.$http)
            this.formdata()
        },
        methods: {
            //初始化表格数据加载
            formdata() {
                let loadding = this.openLoading();
                this.$http.post('Device/GetDeviceCategoryContent', {
                    currentPage: this.pagination.currentPage,
                    pagesize: this.pagination.pagesize,
                    like: this.Like,
                }).then(res => {
                    loadding.close();
                    this.pagination.total = res.data.count
                    this.tableData = res.data.data
                }).catch(() => {
                    this.$message.error('服务器异常,请联系管理员!')
                })
            },
            //序列号
            indexMethod(index) {
                return ((this.pagination.currentPage - 1) * this.pagination.pagesize) + index + 1
            },
            //返回父级分类
            GetParentDeviceCategory(val) {
                for (let i = 0; i < this.AllDeviceCategory.length; i++) {
                    if ((this.AllDeviceCategory)[i].Id == val) {
                        return (this.AllDeviceCategory)[i].DeviceCategoryName
                    }
                }
            },
            //表格数据选择事件
            SelectionChange_event(value) {
                this.SelectMore_lis = value
            },
            //批量删除
            BatchDelete_event() {
                this.$confirm('你确定要删除吗?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消'
                }).then(() => {
                    let arrDel = []
                    if (this.SelectMore_lis.length > 0) {
                        this.SelectMore_lis.forEach(item => {
                            arrDel.push(item.Id)
                        })
                        let loadding = this.openLoading();
                        this.$http.post('Device/DeleteDeviceCategoryContent', arrDel).then(res => {
                            loadding.close();
                            if (res) {
                                this.$message.success('删除成功!')
                                this.SelectMore_lis = []
                                this.formdata()
                            } else {
                                this.$message.success('删除失败,请重试!')
                            }
                        }).catch(() => {
                            this.$message.info('服务器异常,请联系管理员!')
                        })
                    } else {
                        this.$message.warning('没有选中的数据!')
                    }
                }).catch(() => {
                    this.SelectMore_lis = []
                    this.$message.info('已取消!')
                })
            },
            //编辑
            handleClick(obj) {
                this.DeviceCategory_details = obj
                this.UpdataDeviceCategory_dialog = true
            },
            //单个数据删除
            BatchDeleteOne_event(obj) {
                this.SelectMore_lis.push(obj)
                this.BatchDelete_event()
            },
            //添加弹框取消
            AddDeviceCategory_close() {
                this.$refs['AddDeviceCategory_ref'].form_resetField()
            }
        },
    }
</script>

<style scoped>

</style>
